/*
 *  Copyright (c)  2016-NOW  Jacars Co.Ltd.
 *  All rights reserved.
 */
package com.jacars.biz.user.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.jacars.biz.user.dao.model.JsptDict;
import com.jacars.biz.user.dao.service.JsptDictService;
import com.jacars.biz.user.service.DictService;
import com.jacars.biz.user.vo.JsptDictVO;
import com.jacars.common.api.PageResult;
import com.jacars.common.api.Result;
import com.jacars.common.constant.CommonConstant;
import com.jacars.common.utils.BeanUtil;
import com.jacars.common.utils.FuncUtil;
import com.jacars.common.utils.node.ForestNodeMerger;
import com.jacars.common.utils.node.INode;
import com.jacars.db.support.Condition;
import com.jacars.db.support.Query;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

import static com.jacars.biz.user.cache.CacheNames.DICT_LIST;
import static com.jacars.biz.user.cache.CacheNames.DICT_VALUE;


/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/dict")
@Api(value = "字典", tags = "字典")
public class DictController {


	private DictService dictService;

	private JsptDictService jsptDictService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入dict")
	public Result<JsptDictVO> detail(JsptDict dict) {
		//todo 源代码中有好方法DictWrapper
		LambdaQueryWrapper<JsptDict> sysDictLambdaQueryWrapper = new LambdaQueryWrapper<>();
		sysDictLambdaQueryWrapper.eq(JsptDict::getId, dict.getId());
		JsptDict detail = jsptDictService.getOne(sysDictLambdaQueryWrapper);
		JsptDictVO dictVO = BeanUtil.copy(detail, JsptDictVO.class);
		if (FuncUtil.equals(detail.getParentId(), CommonConstant.TOP_PARENT_ID)) {
			dictVO.setParentName(CommonConstant.TOP_PARENT_NAME);
		} else {
			JsptDict parent = jsptDictService.getById(detail.getParentId());
			dictVO.setParentName(parent.getDictValue());
		}
		return Result.succeed(dictVO);
	}

	/**
	 * 列表
	 */
	@GetMapping("/getDictPage")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "列表", notes = "传入dict")
	public PageResult<INode> list(Query query, JsptDict dict) {
		IPage<JsptDict> page = jsptDictService.page(Condition.getPage(query), Condition.getQueryWrapper(dict));
		List<JsptDict> records = page.getRecords();
		long total = page.getTotal();

		List<INode> collect = records.stream().map(k -> BeanUtil.copy(k, JsptDictVO.class)).collect(Collectors.toList());
		List<INode> iNodeList = ForestNodeMerger.merge(collect);

		return PageResult.succeed(iNodeList, total);
	}

	/**
	 * 获取字典树形结构
	 *
	 * @return
	 */
	// @GetMapping("/tree")
	// @ApiOperationSupport(order = 3)
	// @ApiOperation(value = "树形结构", notes = "树形结构")
	// public Result<List<JsptDictVO>> tree() {
	// 	List<JsptDictVO> tree = dictService.tree();
	// 	return Result.succeed(tree);
	// }

	/**
	 * 新增或修改
	 */
	@PostMapping("/create")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "传入dict")
	public Result create(@Valid @RequestBody JsptDict dict) {
		jsptDictService.save(dict);
		return Result.succeed();
		// return Result.succeed(dictService.submit(dict));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增或修改", notes = "传入dict")
	public Result update(@Valid @RequestBody JsptDict dict) {
		return Result.succeed(dictService.submit(dict));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@CacheEvict(cacheNames = {DICT_LIST, DICT_VALUE}, allEntries = true)
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "删除", notes = "传入ids")
	public Result remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		List<JsptDict> list = jsptDictService.list(Wrappers.<JsptDict>lambdaQuery().in(JsptDict::getParentId, ids));
		if (list != null && list.size() > 0) {
			return Result.failed("存在下级子项");
		}
		return Result.succeed(jsptDictService.removeByIds(FuncUtil.toLongList(ids)));
	}

	/**
	 * 获取字典
	 *
	 * @return
	 */
	@GetMapping("/dictionary")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "获取字典", notes = "获取字典")
	public Result<List<JsptDict>> dictionary(String code) {
		List<JsptDict> list = jsptDictService.list(Wrappers.<JsptDict>lambdaQuery().eq(JsptDict::getCode, code));
		return Result.succeed(list);
	}
}
